package com.sskj.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

public class EncryptExcelData {
    private static final String AES_KEY = "VyUngIGEhBUoRdeU"; // AES加密密钥

    private static final String SM4_KEY = "ce89cc651c6c3ae8ce7f9cd332e57efe"; // SM4加密密钥

    private static List<List<String>> dataList = new ArrayList<>(); // 定义数据列表

    public static void main(String[] args) {
        String inputFilePath = "C:\\Users\\Admin\\Desktop\\Test\\formmain_0218(2).xlsx";  // 输入的Excel文件路径
        String outputFilePath = "C:\\Users\\Admin\\Desktop\\Test\\Test1\\formmain_0218(2).xlsx";  // 输出的Excel文件路径

        try {
            List<List<String>> originalData = readExcel(inputFilePath);  // 读取原始Excel数据

            List<List<String>> encryptedData = new ArrayList<>();  // 保存加密后的数据

            for (int i = 0; i < originalData.size(); i++) {
                List<String> encryptedRow = new ArrayList<>();
                for (int j = 0; j < originalData.get(i).size(); j++) {
                    if (j == 28 || j == 29 || j == 34 || j == 35 || j == 36 || j == 37 || j == 62 || j == 61){
                        if (StringUtils.isBlank(originalData.get(i).get(j)) && "NULL".equals(originalData.get(i).get(j)) && "<!NOT NULL>".equals(originalData.get(i).get(j)) && "<!NULL>".equals(originalData.get(i).get(j))){
                            encryptedRow.add(originalData.get(i).get(j));
                            continue;
                        }
                        String encrypt = Sm4Util.encrypt(SM4_KEY, originalData.get(i).get(j));
                        encryptedRow.add(encrypt);
                    }else {
                        encryptedRow.add(originalData.get(i).get(j));
                    }
                }
                encryptedData.add(encryptedRow);
            }
//
//            for (List<String> rowData : originalData) {
//
//                List<String> encryptedRow = new ArrayList<>();
//                for (String cellData : rowData) {
//                    if (StringUtils.isNotBlank(cellData) && !"NULL".equals(cellData) && !"<!NOT NULL>".equals(cellData) && !"<!NULL>".equals(cellData)) {
//                        String encryptedCellData = encryptData(cellData, AES_KEY);
//                        encryptedRow.add(encryptedCellData);
//                    } else {
//                        encryptedRow.add(cellData);
//                    }
//                }
//                encryptedData.add(encryptedRow);
//            }

            writeExcel(outputFilePath, encryptedData);  // 将加密后的数据写入新的Excel文件
            System.out.println("加密完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 使用EasyExcel读取Excel文件
    private static List<List<String>> readExcel(String filePath) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(filePath);
        AnalysisEventListener<Map<Integer, String>> listener = new AnalysisEventListener<Map<Integer, String>>() {
            @Override
            public void invoke(Map<Integer, String> rowData, AnalysisContext context) {
                List<String> rowList = new ArrayList<>();
                for (int i = 0; i < rowData.size(); i++) {
                    rowList.add(rowData.get(i));
                }
                dataList.add(rowList);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
            }
        };
        EasyExcel.read(fileInputStream, null, listener).headRowNumber(0).sheet().doRead();
        return dataList;
    }

    // 使用EasyExcel将数据写入Excel文件
    private static void writeExcel(String filePath, List<List<String>> data) throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream(new File(filePath));
        EasyExcel.write(fileOutputStream).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(data);
    }

    // 使用AES加密算法加密数据
    private static String encryptData(String data, String key) throws Exception {
        Key aesKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encrypted);
//        return new String(encrypted, StandardCharsets.UTF_8);
    }

}
